package helper;

import com.abc.pay.client.ebus.PaymentRequest;
import com.abc.pay.client.ebus.UnifiedPaymentRequest;
import com.google.gson.Gson;
import order.Order;
import play.Logger;
import utils.DateUtil;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.LinkedHashMap;

public class ABCPayHelper {

    public static PaymentRequest initPayment(Order order) {

        PaymentRequest paymentRequest = new PaymentRequest();

        paymentRequest.dicOrder.put("PayTypeID", "ImmediatePay");                   //设定交易类型
        paymentRequest.dicOrder.put("OrderDate", DateUtil.dateToString(order.createdAt , "yyyy/MM/dd"));                  //设定订单日期 （必要信息 - YYYY/MM/DD）
        paymentRequest.dicOrder.put("OrderTime", DateUtil.dateToString(order.createdAt , "HH:mm:ss"));                   //设定订单时间 （必要信息 - HH:MM:SS）
        paymentRequest.dicOrder.put("OrderTimeoutDate", DateUtil.getAfterMinutes(new Date() , 15) );     //设定订单有效期
        paymentRequest.dicOrder.put("CurrencyCode", "156");             //设定交易币种
        paymentRequest.dicOrder.put("OrderNo", order.orderNumber);                       //设定订单编号 （必要信息）
        paymentRequest.dicOrder.put("OrderAmount", new DecimalFormat(".##").format(order.paymentedAmount));      //设定交易金额
//        Logger.info("OrderAmount : %s" , paymentRequest.dicOrder.get("OrderAmount"));
        paymentRequest.dicOrder.put("InstallmentMark", "0");       //分期标识
        paymentRequest.dicOrder.put("CommodityType", "0203");           //设置商品种类

        LinkedHashMap orderitem = new LinkedHashMap();
        orderitem.put("ProductName",  "优粮城电子商务订单");//商品名称
        paymentRequest.orderitems.put(1, orderitem);

        paymentRequest.dicRequest.put("PaymentType", "A");            //设定支付类型
        paymentRequest.dicRequest.put("PaymentLinkType", "2");    //设定支付接入方式
        paymentRequest.dicRequest.put("NotifyType", "1");              //设定通知方式
        paymentRequest.dicRequest.put("ResultNotifyURL", "http://upshan.mynatapp.cc/order/abcpay/callBack");    //设定通知URL地址
        paymentRequest.dicRequest.put("IsBreakAccount", "0");            //设定支付类型
//        Logger.info("paymentRequest : %s" , paymentRequest);
        return paymentRequest;

//        JSON json = paymentRequest.postRequest();
//
//        String ReturnCode = json.GetKeyValue("ReturnCode");
//        String ErrorMessage = json.GetKeyValue("ErrorMessage");
//        if (ReturnCode.equals("0000"))
//        {
//            out.println("ReturnCode   = [" + ReturnCode + "]<br/>");
//            out.println("ErrorMessage = [" + ErrorMessage + "]<br/>");
//            out.println("PaymentURL-->"+json.GetKeyValue("PaymentURL"));
//            response.sendRedirect(json.GetKeyValue("PaymentURL"));
//        }

    }


    /**
     * 农行综合支付接口
     * 主要针对微信
     * @param order
     * @return
     */
    public static UnifiedPaymentRequest unifiedPaymentRequest(Order order) {
        Logger.info("Order ： %s" , new Gson().toJson(order));
        UnifiedPaymentRequest unifiedPaymentRequest = new UnifiedPaymentRequest();
        unifiedPaymentRequest.dicOrder.put("PayTypeID", "JSAPI");                   //设定交易类型
        unifiedPaymentRequest.dicOrder.put("OrderDate", DateUtil.dateToString(order.createdAt , "yyyy/MM/dd"));                  //设定订单日期 （必要信息 - YYYY/MM/DD）
        unifiedPaymentRequest.dicOrder.put("OrderTime", DateUtil.dateToString(order.createdAt , "HH:mm:ss"));                   //设定订单时间 （必要信息 - HH:MM:SS）
        unifiedPaymentRequest.dicOrder.put("OrderTimeoutDate", DateUtil.getAfterMinutes(new Date() , 15) );     //设定订单有效期
        unifiedPaymentRequest.dicOrder.put("CurrencyCode", "156");             //设定交易币种
        unifiedPaymentRequest.dicOrder.put("OrderNo", order.orderNumber);                       //设定订单编号 （必要信息）
        unifiedPaymentRequest.dicOrder.put("OrderAmount", new DecimalFormat(".##").format(order.paymentedAmount));      //设定交易金额
        unifiedPaymentRequest.dicOrder.put("AccountNo" , order.merchant.weixinData.appId);
        unifiedPaymentRequest.dicOrder.put("OpenID" , order.webUser.openId);
        unifiedPaymentRequest.dicOrder.put("OrderDesc" , "8");

//        Logger.info("OrderAmount : %s" , paymentRequest.dicOrder.get("OrderAmount"));
        unifiedPaymentRequest.dicOrder.put("InstallmentMark", "0");       //分期标识
        unifiedPaymentRequest.dicOrder.put("CommodityType", "0203");           //设置商品种类

        LinkedHashMap orderitem = new LinkedHashMap();
        orderitem.put("ProductName",  "优粮城电子商务订单");//商品名称
        unifiedPaymentRequest.orderitems.put(1, orderitem);

        unifiedPaymentRequest.dicRequest.put("PaymentType", "8");            //设定支付类型
        unifiedPaymentRequest.dicRequest.put("PaymentLinkType", "2");    //设定支付接入方式
        unifiedPaymentRequest.dicRequest.put("NotifyType", "1");              //设定通知方式
        unifiedPaymentRequest.dicRequest.put("ResultNotifyURL", "http://upshan.mynatapp.cc/order/abcpay/callBack");    //设定通知URL地址
        unifiedPaymentRequest.dicRequest.put("IsBreakAccount", "0");            //设定支付类型
        return unifiedPaymentRequest;
    }

}
